package com.hanli.longforwords01;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hanli.longforwords01.etymon.EtymonService;
import com.hanli.longforwords01.etymon.EtymonWord;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity {
    //创建一个listView进行关联
    private ListView listView;
    //创建母词列表
    private List<String> etymon=new ArrayList<String>();
    //创建释义列表
    private List<String> exp =new ArrayList<String>();

    private TextView tv_account;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置启动视图
        setContentView(R.layout.activity_main);

        tv_account=(TextView)findViewById(R.id.account);
        Intent intent=getIntent();
        String account = intent.getStringExtra("account");
        tv_account.setText(account);

        //获取到关联表单
        listView=(ListView)findViewById(R.id.lv);
        getEtymons();
        MyBaseAdapter adapter=new MyBaseAdapter();
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //点击跳转到派生词列表
                passData(position);
                //System.out.println("文件编码为："+R.raw.state);
                //System.out.println("文件编码为："+Integer.parseInt(String.valueOf(R.raw.state)));

            }
        });
    }
    //传递母词数据，获取母词id
    public void passData(int position)
    {
        Intent intent=new Intent(this,WordActivity.class);
        intent.putExtra("account",tv_account.getText().toString());
        intent.putExtra("etymon",etymon.get(position));
        startActivity(intent);
    }

    class MyBaseAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return etymon.size();
        }

        @Override
        public Object getItem(int position) {
            return etymon.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }


        @Override
        public View getView(int position, View View, ViewGroup parent) {
            ViewHolder viewHolder;
            if(View==null)
            {
                View=View.inflate(MainActivity.this,R.layout.showetmon,null);

                viewHolder=new ViewHolder();
                viewHolder.word=(TextView)View.findViewById(R.id.etymon);
                viewHolder.exps=(TextView)View.findViewById(R.id.exp);
                viewHolder.rl=findViewById(R.id.show_etymon);

                View.setTag(viewHolder);
            }else
            {
                viewHolder =(ViewHolder) View.getTag();
            }
            viewHolder.word.setText(etymon.get(position));
            viewHolder.exps.setText(exp.get(position));

            return View;
        }
    }

    class ViewHolder
    {
        TextView word;
        TextView exps;
        RelativeLayout rl;
    }
    //调用函数获取到母词

    public void getEtymons()
    {
        InputStream is=this.getResources().openRawResource(R.raw.words);
        try {
            List<EtymonWord> etymonWords= EtymonService.getEtymonFromJson(is);
            for (int i = 0; i <etymonWords.size() ; i++) {
                etymon.add(etymonWords.get(i).getEtymon());
                exp.add(etymonWords.get(i).getExp());
            }
        } catch (IOException e) {
            e.printStackTrace();

        }
    }
}
